package com.gm.baseplatform.base.bootstrap;

import java.io.File;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.gm.baseplatform.base.cfg.ConfigConstant;
import com.gm.baseplatform.base.cfg.Configuration;
import com.gm.baseplatform.base.cfg.IConfiguration;
import com.gm.baseplatform.base.module.Module;
import com.gm.baseplatform.base.module.ModuleManager;
import com.gm.baseplatform.core.utils.ApplicationContextUtil;
import com.gm.baseplatform.core.utils.StringUtil;


public class ModuleBoot implements Boot.BootFlag {

	private Log log = LogFactory.getLog(ModuleBoot.class); 
	
	private Boot boot;
	
	public void boot(Boot boot) throws BootException {
		
		log.info("ModuleBoot...");
		
		this.boot = boot;
		initModuleCfg();
		
	}

	private void initModuleCfg() {
		
		IConfiguration configuration = 
			(IConfiguration)ApplicationContextUtil.getBean("configuration");
		
		Configuration mCfg = configuration.getConfigByName(ConfigConstant.CFG_FLAG_INIT_MODULE);
		if (mCfg != null && ConfigConstant.CFG_INIT_MODULE_OK.equals(mCfg.getValue())) {
			log.info("Module is already...");
			return;
		}
		
		String projectPath = boot.getServletContext().getRealPath("/");
		projectPath += "WEB-INF\\boot\\module_boot.xml";
		
		File file = new File(projectPath);
		if (!file.exists()) {
			return;
		}
		
		SAXReader reader = new SAXReader();
		try {
			Document document = reader.read(file);
			Element modules = document.getRootElement();
			if (modules == null) {
				return;
			}
			
			List<?> moduleList = modules.elements("module");
			if (moduleList == null) {
				return;
			}
			
			for (int i = 0; i < moduleList.size(); i++) {
				Element baseModule = (Element)moduleList.get(i);
				Module baseModuleBean = parseModule(baseModule);
				
				Element cfgModule = baseModule.element("module");
				if (cfgModule == null) {
					return;
				}
				Module cfgModuleBean = parseModule(cfgModule);	
				
				ModuleManager moduleManager = 
					(ModuleManager)ApplicationContextUtil.getBean("moduleManager");
				
				moduleManager.addModule(baseModuleBean);
				if (StringUtil.isNotEmpty(baseModuleBean.getUnid())) {
				   cfgModuleBean.setParent(baseModuleBean.getUnid());
				   moduleManager.addModule(cfgModuleBean);
				}
			}
			
			mCfg.setValue(ConfigConstant.CFG_INIT_MODULE_OK);
			configuration.updateConfig(mCfg);
			
		} catch (DocumentException e) {
			e.printStackTrace();
		}
	}
	
	private Module parseModule(Element module) {
		Module baseModuleBean = new Module();
		baseModuleBean.setName(module.attributeValue("name"));
		baseModuleBean.setImgPath(module.attributeValue("imgpath"));
		baseModuleBean.setPerformAction(module.attributeValue("performaction"));
		baseModuleBean.setPosition(new Integer(module.attributeValue("postion")));
		baseModuleBean.setParent(module.attributeValue("parent"));
		return baseModuleBean;
	}
	
	public String getBootRoot() {
		return Boot.BOOT_ROOT_SERVLET;
	}

}
